package surveyBusiness;

import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/6/10.
 */
public class SurveyUserSave implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        //读取请求json
        JSONObject requestInfo = GetRequestInputStream.getJson(request);
        ReturnJson returnJson = new ReturnJson();

        //数据库操作
        String action = requestInfo.getString("action");

        //保存用户提交的问卷
        if (Objects.equals("saveUserAnswer", action)){
            System.out.println(requestInfo.getJSONObject("data"));
            JSONObject data = requestInfo.getJSONObject("data");
            String uniqueId = data.getString("uniqueid");
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String sql = "SELECT state FROM surveyuser WHERE surveyuniqueid='"+uniqueId+"'";
            System.out.println(sql);
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            //uniqueid是否存在 对应数据库字段 surveyuniqueid
            if (sqlReturnModel.getSelectReturnData().size() == 0){
                returnJson.setErrorMsg("问卷不存在，请先在后管预制");
                returnJson.setApiStatus(0);
                return returnJson.toString();
            }

            //判断问卷是否已经提交过 state=0没有提交过
            String state = sqlReturnModel.getSelectReturnData().get(0).get(0);
            if (state.equals("1")){
                returnJson.setErrorMsg("该问卷已经提交");
                returnJson.setApiStatus(-1);
                return returnJson.toString();
            }

            //更新
            String userName = " username='" + data.getString("name") + "'";
            String contentScore = ",contentscore='" + data.getString("contentScore") + "'";
            String contentAverage = ",contentaverage='" + data.getString("contentAverage") + "'";
            String userMark = ",usermark='" + data.getString("mark") + "'";
            String userState = ",state='1'";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String updateTime = ",updatetime='" + sdf.format(new Date()) + "'";

            sql = "UPDATE surveyuser ";
            String set = "SET " + userName + contentScore + contentAverage + userMark + userState + updateTime;
            String where = " WHERE surveyuniqueId='" + uniqueId + "'";
            sql = sql + set + where;
            System.out.println(sql);
            sqlReturnModel = sqlDispatch.update(sql);
            if (sqlReturnModel.getIsSuccess()){
                returnJson.setResult(1,"提交成功", "");
            }else {
                returnJson.setResult(0,"", sqlReturnModel.getFailingReason());
            }
        }

        return returnJson.toString();
    }


}
